package com.hiof.moraa.server.service;

import com.hiof.moraa.server.dao.AthleteDAO;
import com.hiof.moraa.server.dao.AthleteLiveLocationDAO;
import com.hiof.moraa.server.dao.SupportLiveLocationDAO;
import com.hiof.moraa.server.service.model.LiveLocationFeed;
import java.util.ArrayList;
import java.util.List;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Bergy
 */
@Transactional(propagation = Propagation.REQUIRED)
public class LocationService {

    AthleteLiveLocationDAO athleteLiveLocationDAO;
    SupportLiveLocationDAO supportLiveLocationDAO;
    AthleteDAO athleteDAO;

    public void setAthleteLiveLocationDAO(AthleteLiveLocationDAO athleteLiveLocationDAO) {
        this.athleteLiveLocationDAO = athleteLiveLocationDAO;
    }

    public void setSupportLiveLocationDAO(SupportLiveLocationDAO supportLiveLocationDAO) {
        this.supportLiveLocationDAO = supportLiveLocationDAO;
    }

    public void setAthleteDAO(AthleteDAO athleteDAO) {
        this.athleteDAO = athleteDAO;
    }

    public List<LiveLocationFeed> getLatestLocationOfAllAttendant(int routeId) {
        List<LiveLocationFeed> liveLocationFeeds = new ArrayList<LiveLocationFeed>();
        liveLocationFeeds.addAll(getLatestLocationOfAllAthletes(routeId));
        liveLocationFeeds.addAll(getLatestLocationOfAllSupport(routeId));
        return liveLocationFeeds;
    }

    public List<LiveLocationFeed> getLatestLocationOfAllAthletes(int routeId) {
        List<LiveLocationFeed> liveLocationFeeds = new ArrayList<LiveLocationFeed>();
        liveLocationFeeds.addAll(athleteLiveLocationDAO.getLatestLiveLocationFeedOfAllAthlete(routeId));
        return liveLocationFeeds;
    }
    
    public LiveLocationFeed getLatestLocationOfAthlete(String username, int routeId){
        int athleteId = athleteDAO.getAthleteByUsernameAndRouteId(username, routeId).getId();
        return athleteLiveLocationDAO.getLatestLiveLocationFeedOfOneAthlete(athleteId);
    }

    public List<LiveLocationFeed> getLatestLocationOfAllSupport(int routeId) {
        List<LiveLocationFeed> liveLocationFeeds = new ArrayList<LiveLocationFeed>();
        liveLocationFeeds.addAll(supportLiveLocationDAO.getLatestLiveLocationFeedOfAllSupport(routeId));
        return liveLocationFeeds;
    }

    public List<LiveLocationFeed>  getLocationOfUserInOneRouteByUsername(String username, int routeId) {
        List<LiveLocationFeed> liveLocationFeeds = new ArrayList<LiveLocationFeed>();
        liveLocationFeeds.addAll(athleteLiveLocationDAO.getLiveLocationFeedOfAthleteByUsername(username, routeId));
        liveLocationFeeds.addAll(supportLiveLocationDAO.getLiveLocationFeedOfSupportByUsername(username, routeId));
        return liveLocationFeeds;
    }
}
